﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper.Internal.Mappers;
using ERP.ERPSystem.RBAC.Dto;
using ERP.Team.RBAC;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.RBAC
{
    public class RoleServices : ApplicationService, IRoleServices
    {
        private readonly IRepository<RoleModel, int> _roleServices;
        private readonly IRepository<RolePermissionModel, int> _roleperServices;
        public RoleServices(IRepository<RoleModel, int> roleServices, IRepository<RolePermissionModel, int> roleperServices)
        {
            _roleServices = roleServices;
            _roleperServices= roleperServices;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleDto"></param>
        /// <returns></returns>
        public async Task<int> AddRole(RoleDto roleDto)
        {
            var role = ObjectMapper.Map<RoleDto, RoleModel>(roleDto);
            var i = await _roleServices.InsertAsync(role);
            return i == null ? 0 : 1;
        }

        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleModel>> GetRole()
        {
            var rolelist = await _roleServices.GetListAsync();
            return rolelist;
        }

        /// <summary>
        /// 根据ID获取角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RoleDto> GetRoleById(int id)
        {
            var role = await _roleServices.GetAsync(id);
            return ObjectMapper.Map<RoleModel, RoleDto>(role);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteRole(int id)
        {
            await _roleServices.DeleteAsync(id);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roleDto"></param>
        /// <returns></returns>
        public async Task<int> UpdateRole(RoleDto roleDto)
        {
            var role = ObjectMapper.Map<RoleDto, RoleModel>(roleDto);
            var updatedRole = await _roleServices.UpdateAsync(role);
            return updatedRole == null ? 0 : 1;
        }
        /// <summary>
        /// 角色分配权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> RolePermission(RolePermissionDTO rolePermissionDTO)
        {

            try
            {
                await _roleperServices.DeleteDirectAsync(m => m.RoleId.Equals(rolePermissionDTO.RoleId));
                var roleper = new List<RolePermissionModel>();
                foreach (var item in rolePermissionDTO.PermissionId)
                {
                    roleper.Add(new RolePermissionModel
                    {

                        RoleId = rolePermissionDTO.RoleId,
                        PermissionId = item

                    });
                }
                await _roleperServices.InsertManyAsync(roleper);
                return 1;
            }
            catch (Exception)
            {

                return 0;
            }
        }
        /// <summary>
        /// 反填角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<int>> FantianRolePermission(int id)
        {
            var roleper=await _roleperServices.GetListAsync(m=>m.RoleId.Equals(id));
            var pidlist=roleper.Select(x=>x.PermissionId).ToList();
            return pidlist;
        }
    }
}
